var totalPage = 0;
var totalCnt = 0;
var deleteFlag;
var userInfos = [];
var branchCompanyData = [];
var roleData = [];
//var chargingStationData = [];
var parentNameData = new Array();

$(document).ready(function() {
	showLoading();
	$('title', parent.document).html($('title').text());
	setBaseUrl();
	createTableHead("userManageThead", userManageTable, search);
//	$ajaxList('chargingStation', getChargingStation);
	$("li.leftClick").each(function(){
	    $(this).click(function(){
	    	var id = $(this).attr('id');
	    	$(this).find('img#userSelectRight').toggle();
	    	$(this).find('img#userSelectBottom').toggle();
	    	if (id == 'BranchCompanyList') {
	    		if (branchCompanyData && branchCompanyData.length == 0) {
	    			$ajaxList('branchCompany', getBranchCompany);
	    		}
	    		$('#BranchCompany').toggle();
	    		$('#Role').hide();
	    		$('#RoleList').find('img#userSelectRight').show();
	    		$('#RoleList').find('img#userSelectBottom').hide();
	    	} else if (id == 'RoleList') {
	    		if (roleData && roleData.length == 0) {
	    			$ajaxList('role', getRoleID);
	    		}
	    		$('#BranchCompany').hide();
	    		$('#BranchCompanyList').find('img#userSelectRight').show();
	    		$('#BranchCompanyList').find('img#userSelectBottom').hide();
	    		$('#Role').toggle();
	    	}
	    }) ;
    }) ;
	search();
});

function getRoleID(data) {
	var length = data.length;
	roleData = data;
	for (var i = 0; i < length; i++) {
		$("#Role").append('<li id="roleID'+ i +'" onclick="roleIDVal(\''+ data[i].roleID +'\',\'RoleID\',\''
				+ data[i].roleName +'\',\'roleID'+ i +'\',\'\')">'+ data[i].roleName +'</li>');
	}
}
function getBranchCompany(data) {
	var length = data.length;
	branchCompanyData = data;
	for (var i = 0; i < length; i++) {
		$("#BranchCompany").append('<li id="branchCompanyID'+ i +'" onclick="roleIDVal(\''+ data[i].branchCompanyID +'\',\'BranchCompanyID\',\''
				+ data[i].branchCompanyName + '\',\'branchCompanyID'+ i +'\',\'\')">'+ data[i].branchCompanyName +'</li>');
	}
}
//function getChargingStation(data) {
//	var length = data.length;
//	chargingStationData = data;
//	for (var i = 0; i < length; i++) {
//		$("#ChargingStation").append('<li id="chargingStationID'+ i +'" onclick="roleIDVal(\''+ data[i].chargingStationID +'\',\'ChargingStationID\',\''
//				+ data[i].chargingStationName +'\',\'chargingStationID'+ i +'\',\'\')">'+ data[i].chargingStationName +'</li>');
//	}
//}
function search() {
	$ajax('searchUser', 'searchUserManage', searchUserManage);
}
function roleIDVal(value, valueID, valueName, clickID, flag) {
	formReset("searchUserManage");
	$("#searchName").html(valueName);
	$("#" + valueID).val(value);
	$(".clickBgBlue").removeClass("clickBgBlue");
	if (clickID != '') {
		$("#" + clickID).addClass("clickBgBlue");
	}
	if ("allUser" != clickID) {
		$("#SearchAllUser").val(1);
	}
	if (flag != "") {
		 $(".leftClick").next().children().hide();
		 $('.userSelectRight').show();
		 $('.userSelectBottom').hide();
	}
	search();
}
function userFiltrate(value, valueID){
	formReset("searchUserManage");
	if (value != '') {
		if (valueID != '') {
			$("#" + valueID).val(value);
		}
	}
	search();
}
function nameValue() {
	var userName = $("#name").val();
	$("#UserName").val(userName);
	$("#searchName").html(userName || "用户");
	search();
}
function searchUserManage(data) {
	totalPage = data && data.totalPage || 0;
	totalCnt = data && data.totalCnt || 0;
	userInfos = data && data.userInfos || [];
	if (totalCnt > 1 && userInfos.length > 0) {
		$("#searchCount").html(" "+ totalCnt + "人");
	} else {
		$("#searchCount").html("");
	}
	createTableBody("userManageTbody", userManageTable, userInfos);
	$("#chkAll").hide();
	var checkbox =document.getElementsByName("chk"); 
	for (var i=0; i<checkbox.length; i++) {
		checkbox[i].style.display = "none";
	}
	$(function(){
		$("#name").autocomplete({
			source: parentNameData,
			minLength: 0
		});
	});
	var length = userInfos.length;
	for (var i = 0; i < length; i++) {
		parentNameData[i] = userInfos[i].userName;
	}
	setTimeout(hideLoading, 500);
}

function details(index){
	window.parent.detalisSkip('userManage/userManageDetail',index,$("#currentPage").val());
	$("#parentSkipMenu", parent.document).val("chargingStationPage");
	$("#detailID", parent.document).val(userInfos[index].userID);
}
function chkBefore(index, chkFlag){
	var checkbox =document.getElementsByName("chk"); 
	for (var i=0; i<checkbox.length; i++) {
		checkbox[i].checked = false; 
	}
	checkbox[index].checked = true; 
	if (chkFlag == "update") {
		update(index);
	} else if(chkFlag == "delete") {
		del();
	} else if(chkFlag == "updatePwd") {
		updatePassword(index);
	}
}
//添加
function save() {
	window.parent.skipSecondMenu('userManage/userManageSave', 'userMangeLi', 'userPage')
}
//修改
function update(index) {
	window.parent.detalisSkip('userManage/userManageUpdate',index,$("#currentPage").val());
	$("#parentSkipMenu", parent.document).val("userPage");
	$("#detailID", parent.document).val(userInfos[index].userID);
}
function confirm(){
	return checkForm("updatePasswordForm");
}
function getUrl(){
	if(deleteFlag == "delete"){
		return 'deleteUser';
	} else if(deleteFlag == "updatePassword"){
		return 'updatePasswordInit';
	}
}
function saveUser(data){
	formReset("updatePasswordForm");
	var result = data && data.result;
	if (result == "Success"){
		showMessge("修改成功！");
		$("#updatePassWord").modal("hide");
		search();
	} else if (result == "NotFound"){
		showMessge("修改失败，该用户不存在或原密码错误！");	
	} else{
		showMessge("修改异常，联系管理员！")
	}
}
function getData(){
	return {userID: getCheckDataList(userInfos, "userID")};
}

function sure(data){
	var result = data && data.result;
	if (result == "Success") {
		showMessge(messageContent["Delete.Success"]);
	} else if (result == "NotFound") {
		showMessge(messageContent["Delete.NotFound"]);
	} else {
		showMessge(messageContent["Delete.Error"]);
	}
	$ajax('searchUser', 'searchUserManage', searchUserManage);
}

//修改密码弹出框
function updatePassword(index){
	deleteFlag = "updatePassword";
	$("#updatePassWord").modal("show");
	$("#pwUserID").val(userInfos[index].userID).attr("disabled",true);
}
//确认修改
function sureUpdatePassword(){
	var newPassword = $("#newPassword").val();
	var surePassword = $("#surePassword").val();
	if (newPassword == surePassword) {
		var checkbox = getCheckbox("chk");
		var index = checkbox && checkbox.row_index;
		$ajax(getUrl, {UserID: userInfos[index].userID, Password: $.md5($("#newPassword").val())}, saveUser, confirm);
	} else {
		showMessge("两次输入密码不一致，请重新输入！");
		formReset("updatePasswordForm");
	}
}
